﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Taobao.EF;
using Taobao.Models;

namespace Taobao.Public
{
    public class CustomClass
    {
        /// <summary>
        /// json 转list
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonText"></param>
        /// <returns></returns>
        public static List<T> JsonToList<T>(string jsonText)
        {
            List<T> list = new List<T>();

            DataContractJsonSerializer _Json = new DataContractJsonSerializer(list.GetType());
            byte[] _Using = System.Text.Encoding.UTF8.GetBytes(jsonText);
            System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_Using);
            _MemoryStream.Position = 0;

            return (List<T>)_Json.ReadObject(_MemoryStream);
        }
        /// <summary>
        ///转换为 ProductInfo
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static ProductInfo ConvertToProIn<T>(T entity, int userId)
        {
            Type pitype = typeof(ProductInfo);
            Type enT = typeof(T);
            PropertyInfo[] pi = pitype.GetProperties();
            PropertyInfo[] entPi = enT.GetProperties();
            ProductInfo proi = new ProductInfo();
            foreach (PropertyInfo piItem in pi)
            {
                foreach (PropertyInfo enItem in entPi)
                {
                    if (piItem.Name == enItem.Name)
                    {
                        piItem.SetValue(proi, enItem.GetValue(entity, null), null);

                    }
                }
            }
            proi.UserId = userId;
            proi.searchUrl = "https:" + proi.searchUrl;
            return proi;
        }
        /// <summary>
        /// 保存缓存信息 绝对缓存时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        public static void InsertCache(string key, object val, int mins, int month = 0)
        {
            var dt = DateTime.UtcNow.AddMinutes(mins);
            if (month != 0)
            {
                dt = DateTime.UtcNow.AddMonths(month);
            }
            HttpContext.Current.Cache.Insert(key, val, null, dt, System.Web.Caching.Cache.NoSlidingExpiration);
        }
        /// <summary>
        /// 保存缓存  滑动缓存 从最后一次访问计时
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <param name="mins"></param>
        public static void InserNoabsoCahce(string key, object val, int mins)
        {
            HttpContext.Current.Cache.Insert(key, val, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(mins));
        }
        /// <summary>
        /// 读取缓存
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetCahce(string key)
        {
            return HttpContext.Current.Cache.Get(key);
        }
        /// <summary>
        /// 清理缓存
        /// </summary>
        /// <param name="key"></param>
        public static  void RemoveCahce(string key)
        {
            HttpContext.Current.Cache.Remove(key);
        }

        /// <summary>
        /// 返回基本信息
        /// </summary>
        /// <param name="nm"></param>
        /// <returns></returns>
        public static JsonResult GetJsonRet(NoticeMsg nm, object data = null)
        {
            JsonResult js = new JsonResult();
            BaseModel bm = new BaseModel();
            bm.code = (int)nm;
            bm.data = data;
            bm.msg = nm.ToString();
            js.Data = bm;
            return js;
        }
    }
}